package com.category.controller;

import com.category.common.Result;
import com.category.entity.DateKeyword;
import com.category.service.DateKeywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/keyword")
@CrossOrigin
public class DateKeywordController {
    
    @Autowired
    private DateKeywordService dateKeywordService;
    
    @GetMapping("/list/{date}")
    public Result<List<DateKeyword>> getByDate(@PathVariable String date) {
        List<DateKeyword> keywords = dateKeywordService.getByDate(date);
        return Result.success(keywords);
    }
    
    @GetMapping("/all")
    public Result<List<DateKeyword>> getAllKeywords() {
        List<DateKeyword> keywords = dateKeywordService.getAllKeywords();
        return Result.success(keywords);
    }
    
    @PostMapping("/add")
    public Result<String> addKeyword(@RequestBody DateKeyword dateKeyword) {
        try {
            dateKeywordService.addKeyword(dateKeyword);
            return Result.success("添加成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @PutMapping("/update")
    public Result<String> updateKeyword(@RequestBody DateKeyword dateKeyword) {
        boolean success = dateKeywordService.updateKeyword(dateKeyword);
        return success ? Result.success("更新成功") : Result.error("更新失败");
    }
    
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteKeyword(@PathVariable Long id) {
        boolean success = dateKeywordService.deleteKeyword(id);
        return success ? Result.success("删除成功") : Result.error("删除失败");
    }
}
